package com.zbb.jacocoserver.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.maven.shared.invoker.*;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@Slf4j
public class MavenInvoke {
    public void mavenBuild(String pomPath, List<String> goals, String mavenHome){
        InvocationRequest request = new DefaultInvocationRequest();
        request.setPomFile( new File( pomPath ) );
        request.setGoals( goals );

        Invoker invoker = new DefaultInvoker();
        invoker.setMavenHome(new File(mavenHome));

        /*invoker.setLogger(new PrintStreamLogger(System.err,  InvokerLogger.ERROR){

        } );
        invoker.setOutputHandler(new InvocationOutputHandler() {
            @Override
            public void consumeLine(String s) throws IOException {
            }
        });
        */

        try
        {
            invoker.execute(request);
        }
        catch (MavenInvocationException e) {
            log.error(e.getMessage(),e);
        }
    }
}
